package com.dingwei.superiordiary.PayUtils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.dingwei.superiordiary.bean.OrderPayBean;
import com.dingwei.superiordiary.utils.Constants;
import com.lihao.baselibrary.utils.L;
import com.lihao.baselibrary.utils.T;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created time 2018/4/8.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */
public class WeChatPayUtils {
    IWXAPI api;
    private static WeChatPayUtils instance = null;

    public static WeChatPayUtils getInstance() {
        if (instance == null) {
            instance = new WeChatPayUtils();
        }
        return instance;
    }

    public void setWeChatPay(OrderPayBean.DataBean.WxBean wxBean, PayInterface onPayInterFace, Context context) {
        this.onPayInterFace = onPayInterFace;
        api = WXAPIFactory.createWXAPI(context, Constants.WX_APP_ID);
        api.registerApp(Constants.WX_APP_ID);
        registorPayBroadCast(context);

        PayReq req = new PayReq();
        req.appId = Constants.WX_APP_ID;
        req.partnerId = wxBean.partnerid;
        req.prepayId = wxBean.prepayid;
        req.packageValue = "Sign=WXPay";
        req.nonceStr = wxBean.noncestr;
        req.timeStamp = wxBean.timestamp + "";
        req.sign = wxBean.paysign;

        boolean s = api.sendReq(req);
        if (!s) {
            T.showShort(context, "未安装微信，请选择其他支付方式");
        } else {
        }
    }

    private void registorPayBroadCast(Context context) {

        // 生成广播处理
        mPayBroadCastRecevier = new PayBroadCastRecevier();
        // 实例化过滤器并设置要过滤的广播
        IntentFilter intentFilter = new IntentFilter(Constants.ACTION_PAY_RESULT);
        // 注册广播
        context.registerReceiver(mPayBroadCastRecevier, intentFilter);

    }


    PayBroadCastRecevier mPayBroadCastRecevier;

    class PayBroadCastRecevier extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            final int code = intent.getIntExtra("code", BaseResp.ErrCode.ERR_COMM);
            L.e("---------->" + code);
            switch (code) {
                case BaseResp.ErrCode.ERR_OK:
                    L.e("支付成功");
                    if (onPayInterFace != null) {
                        onPayInterFace.paySuccess();
                    }
                    break;
                case BaseResp.ErrCode.ERR_COMM:
                    L.e("支付失败");
                    if (onPayInterFace != null) {
                        onPayInterFace.payError();
                    }
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    L.e("取消支付");
                    if (onPayInterFace != null) {
                        onPayInterFace.payCancel();
                    }
                    break;
            }

            if (mPayBroadCastRecevier != null) {
                context.unregisterReceiver(mPayBroadCastRecevier);
            }
        }
    }

    private PayInterface onPayInterFace;


}
